<?php
session_start();
if (isset($_SESSION['friendOp']) && isset($_SESSION['openid'])){
    include '../c/config.php';
    $openidfriend=$_SESSION['friendOp'];
    $openid=$_SESSION['openid'];
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    
    $queryG=$mysqli->query("select tid from ".$tableVer."_group where openid='".$openidfriend."'  limit 0, 1");
    if($queryG->num_rows>0){
        $getG=$queryG->fetch_assoc();
        $tid=$getG['tid'];
    }else{
        exit;
    }
    $queryT=$mysqli->query("select id from ".$tableVer."_team where id='".$tid."'  limit 0, 1");
    if($queryT->num_rows>0){
        $getTeam=$queryT->fetch_assoc();
        $teamId=$getTeam['id'];
        $queryU=$mysqli->query("select headimg,nickname from ".$tableVer."_users where openid='".$_SESSION['openid']."' limit 0, 1");
        $getU=$queryU->fetch_assoc();
       
        $mysqli->query("delete from ".$tableVer."_group where openid='".$openid."' ");
        $mysqli->query("insert into ".$tableVer."_group(openid,tid,headimg,nickname,createtime) values('".$openid."','".$teamId."','".$getU['headimg']."','".$getU['nickname']."','".date("Y-m-d H:i:s")."') ");
        $_SESSION['friendOp']='';
    }
    
    $queryC=$mysqli->query("select count(id) as count from ".$tableVer."_group where tid='".$teamId."'  ");
    $getCount=$queryC->fetch_assoc();
    $mysqli->query("update ".$tableVer."_team set renshu=".$getCount['count']." where id='".$teamId."' ");
    
    echo json_encode(array('s'=>'y'));
}
?>